module.exports = function ($scope, $state, $resource, $modal, toaster, $stateParams) {
	$scope.myKeyup = function (e) {
		//IE 编码包含在window.event.keyCode中，Firefox或Safari 包含在event.which中
		var keycode = window.event ? e.keyCode : e.which;
		if (keycode == 13) {
			$scope.loadlist();
		}
  };
  var wxuid = 0;
  $scope.pwxuid = $stateParams.pwxuid;
  $scope.level = $stateParams.level;
  console.log('pwxuid' + $scope.pwxuid);
  console.log('wxuid' + wxuid, $stateParams.wxuid);
  console.log('level' + $scope.level);
	/* 分页
   * ========================================= */
  $scope.maxSize = 5;            //最多显示多少个按钮
  $scope.bigCurrentPage = 1;      //当前页码
  $scope.itemsPerPage = 10; 
  $scope.searchform = {};
	$scope.loadlist = function (str) {
    if (str != undefined && str == 'search') {
      wxuid = ''; 
    } else if ($scope.level != undefined && $scope.level == '3') {
      wxuid = '';
    } else {
      wxuid = '0';
    }
    var para = {
        pageNo:$scope.bigCurrentPage, 
        pageSize:$scope.itemsPerPage,
        wxuid:wxuid,
        pwxuid:$scope.pwxuid
    };
    para = angular.extend($scope.searchform, para);
		$resource('/api/as/mc/userrole/findZDDownList', {}, {}).save(para, function (res) {
			if (res.errcode === 0) {
        $scope.objs = res.data.results;
        $scope.bigTotalItems = res.data.totalRecord;
			} else {
				toaster.error({ title: '', body: res.errmsg });
			}
		});
	}

  $scope.loadlist();
  
  $scope.getList = function () {
    var para = {
        pageNo:$scope.bigCurrentPage, 
        pageSize:$scope.itemsPerPage,
        state : '0'
    };
    para = angular.extend($scope.searchform, para);
		$resource('/api/as/mc/userrole/getShopApplyList', {}, {}).save(para, function (res) {
			if (res.errcode === 0) {
        $scope.TotalItems = res.data.totalRecord;
			} 
		});
	}

  $scope.getList();
  $scope.seeDistributor = function(){
      console.log('1111');
      var modalInstance = $modal.open({
        template: require('../views/skwxNotDistributorList.html'),
        controller: 'skwxNotDistributorList',
        url: '/skwx/skwxNotDistributorList',
        size: 'lg',
        resolve: {
          // 'aa_type': function () {
          //   return 'skv4';
          // }
        }
      });

      modalInstance.opened.then(function () {// 模态窗口打开之后执行的函数  
      });
      modalInstance.result.then(function (showResult) {
      }, function (reason) {
      });
  }

	//查看下集
	$scope.seeLevel = function (obj) {
      console.log(obj.wxuid);
      $state.go('app.skwxBDistributorList', {'wxuid' : obj.wxuid});
  }
  //查看订单
	$scope.seeOrderList = function (obj) {
      console.log(obj.wxuid);
      $state.go('app.skwxShopOrderList', {'wxuid' : obj.wxuid});
  }
  // 返回一级分销商
  $scope.back = function(obj){
      $state.go('app.skwxADistributorList', {'wxuid' : '0', 'pwxuid' : '', 'level' : ''});
  }
  // 查看上级
  $scope.seeALevel = function(obj){
      $state.go('app.skwxADistributorList', {'wxuid' : '0', 'pwxuid' : obj.pwxuid});
  }
  // 邀请分销商
  var qrcode = '';
  var url = '';
  console.log(window.location.host);
  if (window.location.host == 'https://tsv.juyouhx.com'){
    url = 'https://weixint.juyouhx.com/views//distributor/apply_distributor.html?cid='
  } else {
    url = 'https://dlqt.juyouhx.com/skv4/views/distributor/apply_distributor.html?cid='
  }
  var cid = '';
  var pwx = 0;
  $resource('/api/ac/mc/merchantUserShopService/getcid', {}, {}).save({}, function (res) {
    if (res.errcode === 0) {
      console.log(res.data);
      cid = res.data.cid;
      qrcode = url + cid + '&' + pwx;
      console.log('qrcode' + qrcode);
    } else {
      toaster.error({ title: '', body: res.errmsg });
    }
  });
  $scope.add = function (){
        var modalInstance = $modal.open({
          template: require('../views/skwxOrcode1.html'),
          controller: 'skwxOrcode1',
          size: 'sm',
          resolve: {
            qrcode: function () {
              return qrcode;
            }
          }
        });
        modalInstance.result.then(function () {
          $scope.loadlist();
        });
  }

};